package org.jeecg.modules.demo.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.mes.entity.CamelliaMesOrder;
import org.jeecg.modules.demo.mes.entity.CamelliaMesProductRecord;
import org.jeecg.modules.demo.mes.service.ICamelliaMesOrderService;
import org.jeecg.modules.demo.mes.service.ICamelliaMesProductRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Class:BigScreen
 * @Description:@TODO
 * @Author: author
 * @Date: 2023/3/1 19:02
 */

@RestController
@RequestMapping("/BigScreen")
@Slf4j
public class BigScreenController {

    @Autowired
    private ICamelliaMesOrderService orderService;

    @Autowired
    private ICamelliaMesProductRecordService recordService;

    @ApiOperation(value = "订单表-分页列表查询", notes = "订单表-分页列表查询")
    @GetMapping(value = "/order_list")
    public Result<IPage<CamelliaMesOrder>> queryPageList(
            @RequestParam(name = "size", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        QueryWrapper<CamelliaMesOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        Page<CamelliaMesOrder> page = new Page<CamelliaMesOrder>(1, pageSize);
        IPage<CamelliaMesOrder> pageList = orderService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class produceVo {
        String date;
        double val;
    }


    @ApiOperation(value = "日产量", notes = "日产量")
    @GetMapping(value = "/day")
    public List<produceVo> RecordNum(
            @RequestParam(name = "day", defaultValue = "1") Integer day,
            HttpServletRequest req) {
        List<produceVo> list = new ArrayList<>();

        long current = System.currentTimeMillis();    //当前时间毫秒数
        long zero = current;
        Date today = new Date(zero);
        Date today_end = new Date(zero + 1000 * 60 * 60 * 24 - 1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date_str = simpleDateFormat.format(today);
        Date start_date = new Date(zero);
        Date end_date = new Date(zero + 1000 * 60 * 60 * 24 - 1);
        QueryWrapper<CamelliaMesProductRecord> queryWrapper = new QueryWrapper<>();
        final Date final_start_date2 = start_date;
        final Date final_end_date2 = end_date;
        queryWrapper.and(wrapper -> wrapper.between("create_time", final_start_date2, final_end_date2));
        double val = 0;
        List<CamelliaMesProductRecord> list1 = recordService.list(queryWrapper);
        for (CamelliaMesProductRecord record : list1) {
            val += record.getOrderNum();
        }
        produceVo vo = new produceVo();
        vo.setDate(date_str);
        vo.setVal(val);
        list.add(vo);
        for (int i = 0; i < day - 1; i++) {
            zero = zero - 1000 * 60 * 60 * 24;
            start_date = new Date(zero);
            final Date final_start_date = start_date;
            end_date = new Date(zero + 1000 * 60 * 60 * 24 - 1);
            final Date final_end_date = end_date;
            today = new Date(zero);
            today_end = new Date(zero + 1000 * 60 * 60 * 24 - 1);
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date_str = simpleDateFormat.format(today);
            queryWrapper = new QueryWrapper<>();
            queryWrapper.and(wrapper -> wrapper.between("create_time", final_start_date, final_end_date));
            val = 0;
            list1 = recordService.list(queryWrapper);
            for (CamelliaMesProductRecord record : list1) {
                val += record.getOrderNum();
            }
            vo = new produceVo();
            vo.setDate(date_str);
            vo.setVal(val);
            list.add(vo);
        }
        return list;
    }
}
